<template>
  <div class="ma-content-block lg:flex justify-between p-4">
    <!-- CRUD 组件 -->
    <ma-crud :options="options" :columns="columns" ref="crudRef">

      <template #create_time="{ record }">
        {{ tool.dateFormat(record.create_time, 'yyyy-MM-dd hh:mm:ss', '-') }}
      </template>
      <template #operationBeforeExtend="{ record }">
        <a-link v-if="record.mode_type==1" @click="openProductList(record)">
          <icon-menu/>
          关联产品
        </a-link>

        <a-link v-if="record.mode_type==2" @click="openProductList1(record)">
          <icon-menu/>
          关联产品
        </a-link>

        <a-link v-if="record.mode_type==3" @click="openProductList(record)">
          <icon-menu/>
          关联产品
        </a-link>
      </template>
      <template #img="{ record }">
        <a-avatar shape="square">
          <img :src="record.img?record.img:'/nopic.png'" style="object-fit: cover"/>
        </a-avatar>
      </template>
      <template #tableAfterButtons>
        <a-button type="primary" status="warning" @click="selectOperation('updatePrice',1)">更新变价箱</a-button>
        <a-button type="primary" status="success" @click="selectOperation('updateGood',2)">更新变物箱</a-button>
        <a-button type="primary" @click="selectOperation('updateActivity',3)">更新活动箱子</a-button>
      </template>
    </ma-crud>
    <product-list ref="productListRef"/>
    <product-list1 ref="productList1Ref"/>
  </div>
</template>
<script setup>
import {ref, reactive} from 'vue'
import boxInfo from '@/api/box/boxInfo'
import {Message, Modal} from '@arco-design/web-vue'
import tool from '@/utils/tool'
import * as common from '@/utils/common'

import productList from './components/product.vue'
import productList1 from './components/product1.vue'
import userInfo from "@/api/user/userInfo.js";


const crudRef = ref()

const productListRef = ref()
const productList1Ref = ref()

const selectOperation = (value, type) => {
  let message = '';
  let title = '';
  if (type == 1) {
    message = '确定所有箱子变价模式?';
    title = '变价模式'
  } else if (type == 2) {
    message = '确定所有箱子变物模式?';
    title = '变物模式'
  } else {
    message = '确定所有活动箱子更新';
    title = '更新活动模式'
  }
  Modal.info({
    title: '提示:' + title,
    content: message,
    simple: false,
    onBeforeOk: (done) => {
      updateBoxMode(type)
      done(true)
    }
  })
  return
}

const updateBoxMode = (type) => {
  boxInfo.updateBoxMode({type}).then(res => {
    res.success && Message.success(res.message)
    updateSuccess();
  })
}
const openProductList = (row) => {
  productListRef.value.open(row);
}
const openProductList1 = (row) => {
  productList1Ref.value.open(row);
}


const numberOperation = (newValue, id, numberName) => {
  boxInfo.numberOperation({id, numberName, numberValue: newValue}).then(res => {
    res.success && Message.success(res.message)
  }).catch(e => {
    console.log(e)
  })
}

const switchStatus = (statusValue, id, statusName) => {
  boxInfo.changeStatus({id, statusName, statusValue}).then(res => {
    res.success && Message.success(res.message)
  }).catch(e => {
    console.log(e)
  })
}
const handleSearch = (value) => {
  console.log(value)
}

const options = reactive({
  id: 'us_box_info',
  bordered: {wrapper: true, cell: true},
  rowSelection: {
    showCheckedAll: true
  },
  pk: 'id',
  operationColumn: true,
  operationColumnWidth: 220,
  formOption: {
    viewType: 'modal',
    width: 600
  },
  api: boxInfo.getList,
  recycleApi: boxInfo.getRecycleList,
  add: {
    show: true,
    api: boxInfo.save,
    auth: ['box:info:save']
  },
  edit: {
    show: true,
    api: boxInfo.update,
    auth: ['box:info:update']
  },
  delete: {
    show: true,
    api: boxInfo.deletes,
    auth: ['box:info:delete'],
    realApi: boxInfo.realDeletes,
    realAuth: ['box:info:realDeletes']
  },
  recovery: {
    show: true,
    api: boxInfo.recoverys,
    auth: ['box:info:recovery']
  }
})

const columns = reactive([
  {
    title: "ID",
    dataIndex: "id",
    formType: "input",
    addDisplay: false,
    editDisplay: false,
    width: 60,
    commonRules: {
      required: true,
      message: "请输入ID"
    }
  },


  {
    title: "盒子名称",
    dataIndex: "name",
    width: 280,
    formType: "input",
    search: true,
    commonRules: {
      required: true,
      message: "请输入盒子名称"
    }
  },
  {
    title: "分类名称",
    dataIndex: "category_id",
    width: 180,
    formType: "select",
    dict: {url: 'box/category/list', props: {label: 'name', value: 'id'}, translation: true},
    commonRules: {
      required: true,
      message: "请输入盒子分类id"
    }
  },
  {
    title: "价格",
    dataIndex: "price",
    formType: "input-number",
    min: 0,
    precision: 2,
    commonRules: {
      required: true,
      message: "请输入盒子价格"
    }
  },
  {
    title: "背景图",
    dataIndex: "img",
    formType: "upload",
    onlyData: true,
    returnType: "url",
    multiple: false,
    chunk: false,
    type: "image"
  },
  {
    title: "盒子风格",
    dataIndex: "box_style_id",
    formType: "select",
    dict: {url: 'box/style/list', props: {label: 'name', value: 'id'}, translation: true},
  },
  {
    title: "排序",
    dataIndex: "sort",
    formType: "input-number",
    commonRules: {
      required: true,
      message: "请输入排序"
    }
  },
  {
    title: "模式",
    dataIndex: "mode_type",
    formType: "radio",
    dict: {
      name: "box_mode_type",
      props: {
        label: "title",
        value: "key"
      },
      translation: true
    },
    onControl: (val, maFormObject) => {
      const service = maFormObject.getColumnService()
      if (val == 1) {
        service.get('price_coefficient').setAttr('display', true)
        service.get('activity_num').setAttr('display', false)
        service.get('activity_price').setAttr('display', false)
        service.get('activity_start').setAttr('display', false)
        service.get('activity_end').setAttr('display', false)
      } else if (val == 2) {
        service.get('price_coefficient').setAttr('display', true)
        service.get('activity_num').setAttr('display', false)
        service.get('activity_price').setAttr('display', false)
        service.get('activity_start').setAttr('display', false)
        service.get('activity_end').setAttr('display', false)
      } else if (val == 3) {
        service.get('price_coefficient').setAttr('display', false)
        service.get('activity_num').setAttr('display', true)
        service.get('activity_price').setAttr('display', true)
        service.get('activity_start').setAttr('display', true)
        service.get('activity_end').setAttr('display', true)
      }
    },
    addDefaultValue: '1',
    search: true
  },

  {
    title: "售价系数",
    dataIndex: "price_coefficient",
    formType: "input-number",
    min: 0.1,
    max:1,
    precision: 2,
    addDefaultValue: 1
  },
  {
    title: "活动次数",
    dataIndex: "activity_num",
    formType: "input"
  },
  {
    title: "活动充值",
    dataIndex: "activity_price",
    formType: "input"
  },
  {
    title: "初始时间",
    dataIndex: "activity_start",
    width: 180,
    formType: "date",

    showTime: true,
    range: false
  },

  {
    title: "结束时间",
    dataIndex: "activity_end",
    width: 180,
    formType: "date",

    showTime: true,
    range: false
  },
  {
    title: "状态",
    dataIndex: "status",
    formType: "radio",
    search: true,
    commonRules: {
      required: true,
      message: "请输入展示的箱子"
    },
    dict: {
      name: "data_show_status",
      props: {
        label: "title",
        value: "key"
      },
      translation: true
    },
    addDefaultValue: '1',
  },

  {
    title: "创建时间",
    dataIndex: "create_time",
    width: 180,
    formType: "range",
    search: true,
    addDisplay: false,
    editDisplay: false,
    showTime: true
  },

  {
    title: "更新时间",
    dataIndex: "update_time",
    formType: "input",
    addDisplay: false,
    editDisplay: false,
    hide: true
  },
  {
    title: "删除",
    dataIndex: "delete_time",
    formType: "input",
    addDisplay: false,
    editDisplay: false,
    hide: true
  }
])
</script>
<script> export default {name: 'box:info'} </script>